# -*- coding:utf8 -*-
"""
达观基础NLP体验接口
"""
import requests
import json
from request_utility.util import parse_packed_header

POS_MAP = {"n": "名词", "nr": "人名", "ns": "地名", "nt": "机构团体", "nz": "其他专名", "t": "时间词", "s": "处所词", "f": "方位词", "v": "动词", "a": "形容词", "b": "区别词", "z": "状态词", "r": "代词", "m": "数词", "q": "量词", "d": "副词", "p": "介词", "c": "连词", "u": "助词", "y": "语气词", "o": "拟声词", "h": "前缀", "k": "后缀", "w": "标点"}


def pos(content):
    """
    分词以及词性标注
    :param content:
    :return:
    """
    url = "http://fileload.datagrand.com:8080/pos"
    header = parse_packed_header("""Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36""")
    ret = requests.post(url, data={'text': content})
    return ret.json()


def ner(content):
    """
    实体识别
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/ner'
    header = parse_packed_header("""
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36""")
    ret = requests.post(url, data={'text': content})
    return ret.json()


def summary(content):
    """
    摘要
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/summary'
    header = parse_packed_header("""
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36""")
    ret = requests.post(url, data={'text': content, 'cnt':2}) # 2,3,4,5分别为20%,30%,40%,50%
    return ret.json()


def sentiment(content):
    """
    摘要
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/sentiment'
    header = parse_packed_header("""
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36""")
    ret = requests.post(url, data={'text': content})
    return ret.json()


def classify(content):
    """
    摘要
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/classify'
    header = parse_packed_header("""
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Length:686
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36""")
    ret = requests.post(url, data={'text': content})
    return ret.json()


def tag(content):
    """
    摘要
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/tag'
    header = parse_packed_header("""
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Length:686
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36
""")
    ret = requests.post(url, data={'text': content})
    return ret.json()


def relation(content):
    """
    摘要
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/relation'
    header = parse_packed_header("""
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Length:706
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36
""")
    ret = requests.post(url, data={'text': content, 'relation_weight': 0.6})
    return ret.json()


def audit(content):
    """
    摘要
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/audit'
    header = parse_packed_header("""
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Length:706
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36
""")
    ret = requests.post(url, data={'text': content, 'relation_weight': 0.6})
    return ret.json()


def opinion(content):
    """
    摘要
    :param content:
    :return:
    """
    url = 'http://fileload.datagrand.com:8080/opinion'
    header = parse_packed_header("""
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Length:706
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:fileload.datagrand.com:8080
Origin:http://www.datagrand.com
Referer:http://www.datagrand.com/demo/nlp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/62.0.3202.94 Chrome/62.0.3202.94 Safari/537.36
""")
    ret = requests.post(url, data={'text': content, 'relation_weight': 0.6})
    return ret.json()


if __name__ == '__main__':
    ret = opinion("一九九九年，刘德华去了日本，在国际原子能研究中心当试验品！")
    print(json.dumps(ret, ensure_ascii=False, indent=2))
